package com.wpen.unit.service.job.impl;

import java.util.Date;
import java.util.List;

import com.wpen.unit.service.job.JobInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wpen.unit.cvt.job.JobInfoCvt;
import com.wpen.unit.enums.YN;
import com.wpen.unit.exception.BizException;
import com.wpen.unit.infra.mybatis.job.po.JobInfoPo;
import com.wpen.unit.infra.repository.job.JobInfoRepository;
import com.wpen.unit.web.dto.job.JobInfoAddRequest;
import com.wpen.unit.web.dto.job.JobInfoAddResponse;
import com.wpen.unit.web.dto.job.JobInfoListRequest;
import com.wpen.unit.web.dto.job.JobInfoListResponse;
import com.wpen.unit.web.dto.job.JobInfoListRowsResponse;
import com.wpen.unit.web.dto.job.JobInfoModRequest;
import com.wpen.unit.web.dto.job.JobInfoModResponse;

@Service
public class JobInfoServiceImpl implements JobInfoService {

    @Autowired
    private JobInfoRepository jobInfoRepository;

    @Override
    public JobInfoListResponse queryListJobInfo(JobInfoListRequest jobInfoListRequest){
        int index = jobInfoListRequest.getIndex();
    	int size = jobInfoListRequest.getSize();
        // 数据库查询
        JobInfoPo jobInfoPo = JobInfoCvt.INSTANCE.queryListDto2Do(jobInfoListRequest);

        long total = jobInfoRepository.count(jobInfoPo);
        List<JobInfoPo> jobInfoPoLists = jobInfoRepository.queryList(index, size,jobInfoPo);

        // 返回响应信息
        JobInfoListResponse jobInfoListResponse = new JobInfoListResponse();
        jobInfoListResponse.setTotal(total);
		jobInfoListResponse.setCount(0);
        if (jobInfoPoLists != null) {
            jobInfoListResponse.setCount(jobInfoPoLists.size());
            List<JobInfoListRowsResponse> listRows = jobInfoListResponse.getRows();
            for (JobInfoPo inJobInfoPo : jobInfoPoLists) {
                JobInfoListRowsResponse row = JobInfoCvt.INSTANCE.queryListRowDo2Dto(inJobInfoPo);
                // row.setId(inJobInfoPo.getId());
                // row.setBizCode(inJobInfoPo.getBizCode());
                // row.setBatchCode(inJobInfoPo.getBatchCode());
                // row.setTaskCode(inJobInfoPo.getTaskCode());
                // row.setJobCode(inJobInfoPo.getJobCode());
                // row.setOrders(inJobInfoPo.getOrders());
                // row.setEnabled(inJobInfoPo.getEnabled());
                // row.setJobDesc(inJobInfoPo.getJobDesc());
                // row.setFailIgnore(inJobInfoPo.getFailIgnore());
                // row.setNormParams(inJobInfoPo.getNormParams());
                // row.setNormShardingParams(inJobInfoPo.getNormShardingParams());
                // row.setThreadRegBean(inJobInfoPo.getThreadRegBean());
                // row.setThreadExeBean(inJobInfoPo.getThreadExeBean());
                // row.setThreadCheckBean(inJobInfoPo.getThreadCheckBean());
                // row.setThreadConcurrentMax(inJobInfoPo.getThreadConcurrentMax());
                // row.setAddTime(inJobInfoPo.getAddTime());
                // row.setVersion(inJobInfoPo.getVersion());
                // row.setTimeStamp(inJobInfoPo.getTimeStamp());
                listRows.add(row);
            }
            jobInfoListResponse.setRows(listRows);
        }

        return jobInfoListResponse;
    }


    @Override
    public JobInfoAddResponse addJobInfo(JobInfoAddRequest jobInfoAddRequest ) {
        // 持久化操作
        JobInfoPo jobInfoPo = JobInfoCvt.INSTANCE.addDto2Do(jobInfoAddRequest);
         jobInfoPo.setEnabled(YN.No.value());
         jobInfoPo.setCreateTime(new Date());
        jobInfoPo.setVersion(1);
        //jobInfoPo.setTimeStamp(new Date());
        //保存数据
        jobInfoRepository.insert(jobInfoPo);

        // 返回响应信息
        JobInfoAddResponse jobInfoAddResponse = new JobInfoAddResponse();
        return jobInfoAddResponse;
    }



    @Override
    public JobInfoModResponse modJobInfo(JobInfoModRequest jobInfoModRequest ){
        // 持久化操作
        JobInfoPo jobInfoPo = JobInfoCvt.INSTANCE.modDto2Do(jobInfoModRequest);
	    // 查询数据是否存在
        JobInfoPo jobInfoPoDb = jobInfoRepository.queryById(jobInfoPo.getId());
        if(jobInfoPoDb == null) {
            throw new BizException("E00001", jobInfoModRequest.getId() + "");
        }
        //更新数据
        // TODO change you need *******mod******** ↘↘↘requirement↙↙↙
        jobInfoPo.setId(jobInfoPoDb.getId());
        // jobInfoPo.setBizCode(jobInfoPoDb.getBizCode());
        // jobInfoPo.setBatchCode(jobInfoPoDb.getBatchCode());
        // jobInfoPo.setTaskCode(jobInfoPoDb.getTaskCode());
        // jobInfoPo.setJobCode(jobInfoPoDb.getJobCode());
        // jobInfoPo.setOrders(jobInfoPoDb.getOrders());
        // jobInfoPo.setEnabled(jobInfoPoDb.getEnabled());
        // jobInfoPo.setJobDesc(jobInfoPoDb.getJobDesc());
        // jobInfoPo.setFailIgnore(jobInfoPoDb.getFailIgnore());
        // jobInfoPo.setNormParams(jobInfoPoDb.getNormParams());
        // jobInfoPo.setNormShardingParams(jobInfoPoDb.getNormShardingParams());
        // jobInfoPo.setThreadRegBean(jobInfoPoDb.getThreadRegBean());
        // jobInfoPo.setThreadExeBean(jobInfoPoDb.getThreadExeBean());
        // jobInfoPo.setThreadCheckBean(jobInfoPoDb.getThreadCheckBean());
        // jobInfoPo.setThreadConcurrentMax(jobInfoPoDb.getThreadConcurrentMax());
        // jobInfoPo.setAddTime(jobInfoPoDb.getAddTime());
        jobInfoPo.setVersion(jobInfoPoDb.getVersion());
        // jobInfoPo.setTimeStamp(jobInfoPoDb.getTimeStamp());
        jobInfoRepository.updateById(jobInfoPo);

        // 返回响应信息
        JobInfoModResponse jobInfoModResponse = new JobInfoModResponse();
        return jobInfoModResponse;
    }


}
